//
//  LGPatientClinicRecordCell.m
//  HeartDiseaseManage
//
//  Created by Bovin on 2023/9/8.
//  诊疗记录

#import "LGPatientClinicRecordCell.h"
#import "LGPatientDetailItemView.h"

@interface LGPatientClinicRecordCell()
/**   */
@property (nonatomic , strong)LGPatientDetailItemView *baseView;
/**   */
@property (nonatomic , strong)UIButton *tipButton;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)UILabel *emptyLabel;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGPatientClinicRecordCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    //共病
    for (UILabel *itemLabel in self.itemArray) {
        [itemLabel removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    CGFloat itemTop = 0;
    CGFloat itemLeft = 0;
    for (NSInteger i=0; i<dataArray.count; i++) {
        NSString *content = LGNSString(dataArray[i]);
        CGFloat textWidth = [content textWidthWithFont:LGFont(10) maxHeight:MAXFLOAT];
        itemTop  = ((itemLeft+textWidth+viewPix(8))>=(Screen_W-viewPix(56))) ? itemTop+viewPix(24):itemTop;
        itemLeft = ((itemLeft+textWidth+viewPix(8))>=(Screen_W-viewPix(56))) ? 0:itemLeft;
       
        UILabel *itemLabel = [UILabel lableWithText:dataArray[i] colorString:@"#4C87FD" textFont:LGFont(10) textAlignment:NSTextAlignmentCenter lines:1];
        itemLabel.frame = CGRectMake(itemLeft, itemTop, textWidth+viewPix(8), viewPix(16));
        itemLabel.layer.borderColor = [UIColor colorWithString:@"#4C87FD"].CGColor;
        itemLabel.layer.borderWidth = 1;
        itemLabel.cornerRidus = viewPix(8);
        [self.itemView addSubview:itemLabel];
        [self.itemArray addObject:itemLabel];
        itemLeft += textWidth+viewPix(16);
    }
    CGFloat itemHeight = (dataArray.count>0) ? itemTop+viewPix(16) : 0;
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(itemHeight+viewPix(5)));
    }];
}

-(void)setShowEmpty:(BOOL)showEmpty{
    _showEmpty = showEmpty;
    self.emptyLabel.hidden = !showEmpty;
    CGFloat emptyTop = (showEmpty == YES)?viewPix(4):0;
    CGFloat emptyBottom = (showEmpty == YES)?viewPix(-35):-viewPix(16);
    [self.emptyLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemView.mas_bottom).offset(emptyTop);
        make.bottom.equalTo(self.baseView).offset(emptyBottom);
    }];
}

-(void)setDiseaseCode:(NSString *)diseaseCode{
    _diseaseCode = diseaseCode;
    self.baseView.title = [[NSObject appType:diseaseCode] isEqualToString:@"HX"]?@"既往史":@"基础病历";
    NSString *title = [[NSObject appType:diseaseCode] isEqualToString:@"HX"]?@"诊疗记录":@"查看全部";
    [self.tipButton setTitle:title forState:UIControlStateNormal];
    [self.tipButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:1];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.tipButton];
    [self.baseView addSubview:self.itemView];
    [self.baseView addSubview:self.emptyLabel];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(viewPix(32));
    }];
    [self.tipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.width.equalTo(@(viewPix(80)));
        make.height.equalTo(@(22));
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(45));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.height.equalTo(@(0));
    }];
   
    [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemView.mas_bottom).offset(viewPix(6));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.bottom.equalTo(self.baseView).offset(-viewPix(32));
    }];
}

-(LGPatientDetailItemView *)baseView{
    if(!_baseView){
        _baseView = [[LGPatientDetailItemView alloc]init];
        [_baseView setShadoWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.title = @"基础病历";
        _baseView.contentLabel.hidden = YES;
    }
    return _baseView;
}

-(UIButton *)tipButton{
    if(!_tipButton){
        _tipButton = [UIButton buttonWithTitle:@"查看全部" titleFont:LGFont(14) textColor:@"#666666" imageName:@"grayTip12"];
        _tipButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _tipButton.bounds = CGRectMake(0, 0, viewPix(80), 22);
        _tipButton.userInteractionEnabled = NO;
        [_tipButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:1];
    }
    return _tipButton;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc]init];
    }
    return _itemView;
}

-(UILabel *)emptyLabel{
    if(!_emptyLabel){
        _emptyLabel = [UILabel lableWithText:@"暂无手术信息" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _emptyLabel;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
